﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BookStore.Admin.Models;
using System.Data;
namespace BookStore.Admin.Controllers
{
    [HandleError]
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            BookModel book = new BookModel();
            UsersModel user = new UsersModel();
            if (Session["Session_Admin"] == null)
            {
                // chuyen den trang logon
                Response.Redirect("/admin/login");

            }

            if (this.Request.QueryString["tungay"] != null && this.Request.QueryString["denngay"] != null)
            {
                try
                {
                    DateTime d_old = new DateTime(1970, 1, 1);
                    string ngaytu = this.Request.QueryString["tungay"];
                    string[] temp = ngaytu.Split(Convert.ToChar("-"));
                    DateTime tungay = new DateTime(Convert.ToInt32(temp[2]), Convert.ToInt32(temp[1]), Convert.ToInt32(temp[0]));

                    string ngayden = this.Request.QueryString["denngay"];
                    temp = ngayden.Split(Convert.ToChar("-"));
                    DateTime denngay = new DateTime(Convert.ToInt32(temp[2]), Convert.ToInt32(temp[1]), Convert.ToInt32(temp[0]));

                    int _tungay = (int)((tungay.Ticks - d_old.Ticks) / 10000000);
                    int _denngay = (int)((denngay.Ticks - d_old.Ticks) / 10000000);

                    ViewData["tungay"] = this.Request.QueryString["tungay"];

                    ViewData["denngay"] = this.Request.QueryString["denngay"];
                    string[][] topbuy = book.getTopBuyBook(_tungay, _denngay);
                    ViewData["topbuy"] = topbuy;

                    string[][] toprent = book.getTopRentBook(_tungay, _denngay);
                    ViewData["toprent"] = toprent;

                    string[][] topfavorite = book.getTopFavoriteBook(_tungay, _denngay);
                    ViewData["topfavorite"] = topfavorite;

                    string[][] topcomment = book.getTopCommentBook(_tungay, _denngay);
                    ViewData["topcomment"] = topcomment;

                    ViewData["total_user"] = Convert.ToInt32(user.getTotalUser(_tungay, _denngay)) > 0 ? user.getTotalUser(_tungay, _denngay) : "0";

                    ViewData["total_book"] = Convert.ToInt32(book.getTotalBook(_tungay, _denngay)) > 0 ? book.getTotalBook(_tungay, _denngay) : "0";

                    ViewData["total_book_sell"] = Convert.ToInt32(book.getTotalBookSell(_tungay, _denngay)) > 0 ? book.getTotalBookSell(_tungay, _denngay) : "0";

                    ViewData["total_sell"] = Convert.ToInt32(book.getTotalSell(_tungay, _denngay)) > 0 ? book.getTotalSell(_tungay, _denngay) : "0";

                    ViewData["total_rent"] = Convert.ToInt32(book.getTotalRent(_tungay, _denngay)) > 0 ? book.getTotalRent(_tungay, _denngay) : "0";
                   
                }
                catch (Exception e)
                {
                    Response.Redirect("/admin/home/index");
                }
            }
            else
            {
                string[][] topbuy = book.getTopBuyBook(0,0);
                ViewData["topbuy"] = topbuy;

                string[][] toprent = book.getTopRentBook(0, 0);
                ViewData["toprent"] = toprent;

                string[][] topfavorite = book.getTopFavoriteBook(0, 0);
                ViewData["topfavorite"] = topfavorite;

                string[][] topcomment = book.getTopCommentBook(0, 0);
                ViewData["topcomment"] = topcomment;

                ViewData["total_user"] = user.getTotalUser(0, 0);

                ViewData["total_book"] = book.getTotalBook(0, 0);

                ViewData["total_book_sell"] = book.getTotalBookSell(0, 0);

                ViewData["total_sell"] = book.getTotalSell(0, 0);

                ViewData["total_rent"] = book.getTotalRent(0, 0);

              

            }
            
            return View();
        }
    }
}
